#Roster Management Class
import xmpp
import os
import signal
import time
import sys
from PyQt4 import QtCore

class Roster(QtCore.QThread):
    def __init__(self,client,parent = None):
        self.client = client

    def run(self):
        self.receiveBuddyRequest()

    # This method retrieves a full contact list and should be updated after a buddy is added / removed
    # and after a buddy request is accepted:
    def getContactList(self):
        # get the roster (contact list)
        self.roster = self.client.getRoster()
        # make a list of contacts:
        self.rosterList= self.roster.getItems()
        # Make a new list of contacts from the roster, with subscriptions only!
        for contact in self.rosterList:
            if self.checkSubscription(contact.getStripped()) is not None:
                self.contactList.append(contact)
        return self.contactList

    # In the roster the getStatus method returns the JID's personal message,
    # NOT the status (ex: online, offline, etc.)
    def getPersonalMessage(self, contactJID):
        realJid = xmpp.protocol.JID(contatcJID)
        contactStatus = self.roster.getStatus(realJID.getStripped())
        if contactStatus == None:
            return ""
        else:
            return contactStatus
    
    # Returns the JID's sign in name
    def getContactName(self, contactJID):
        realJid = xmpp.protocol.JID(contatcJID)
        contactName = self.roster.getName(realJID.getStripped())
        contactEmail = realJID.getStripped()
        
        if contactName == None:
            return contactEmail
        else:
            return contactName 
                
    # This method is used by GoOn, it keeps the connection running
    def StepOn(self, conn):
        try:
            conn.Process(1)
        except KeyboardInterrupt:
            return 0
        return 1
      
    # This method is used by receiveBuddyRequest,
    # it checks for a running connection
    def GoOn(self, conn):
        while self.StepOn(conn):
            pass

    def receiveBuddyRequest(self):
        self.client.RegisterHandler("presence",self.presenceHandler)
        self.GoOn(self.client)
        
    # Checks for an incoming subscription request
    def presenceHandler(self, event):
       '''Handle presence event'''
       fromJID  = xmpp.protocol.JID(event.getFrom())
       presType = event.getType()
       if presType == 'subscribe':
           """
            Must prompt user with request
            Ask use: "Do you want to add " +fromJID.getStripped() +" to cintact list?"
            If yes:
           """
           self.client.sendPresence(fromJID.getStripped(),'subscribed')
           print "buddy added"
           return True
           """
           If not:
           self.client.sendPresence(to=fromJID,typ='unsubscribed')
           return False
           """
    # Adds a buddy to the roster, but shows up on roster list when buddy signs in
    def addBuddy(self, buddyJid):
        realJid = xmpp.protocol.JID(buddyJid)
        self.r.Subscribe(realJid.getStripped())
        
    # Removes buddy from roster, but deleted from roster list when buddy signs in
    def removeBuddy(self, buddyJid):
        realJid = xmpp.protocol.JID(buddyJid)
        self.r.delItem(realJid.getStripped())
        
    # Checks the type of subscription a JID has:
    def checkSubscription(self, buddyJid):
        self.r.getSubscription(buddyJid)
